/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年10月27日
 */
package com.massyframework.beanskin.testsuit.springboot;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.security.ProtectionDomain;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.massyframework.beanskin.framework.Framework;
import com.massyframework.beanskin.framework.FrameworkInitializingEvent;
import com.massyframework.beanskin.launching.instrument.agent.InstrumentationAgent;

/**
 * @author huangkh
 *
 */
@Service
public class AgentTest {
	
	private Framework framework;

	/**
	 * 
	 */
	public AgentTest() {
		
	}
	
	/**
	 * 
	 */
	@PostConstruct
	public void init() {
		Instrumentation inst = InstrumentationAgent.getInstrumentation();
		
		try {
			FrameworkInitializingEvent event = new FrameworkInitializingEvent(framework);
			
			event = null;
			inst.addTransformer(new Transformer(), true);
			
			if (inst.isModifiableClass(FrameworkInitializingEvent.class)) {
				inst.retransformClasses(FrameworkInitializingEvent.class);
			}
		} catch (UnmodifiableClassException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * @return the framework
	 */
	public Framework getFramework() {
		return framework;
	}

	/**
	 * @param framework the framework to set
	 */
	@Resource
	public void setFramework(Framework framework) {
		this.framework = framework;
	}



	private class Transformer implements ClassFileTransformer {

		@Override
		public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
				ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
			
			//System.out.println(className);
			return null;
		}
		
	}

}
